// HeatDistortionManager.ts
import {
    _decorator, Component, RenderTexture, Sprite, SpriteFrame,
    UITransform, Camera, Node, Vec2, Vec3, math, director, game, resources, Material, Color,
    Vec4,
    v4
} from 'cc';
const { ccclass, property } = _decorator;

@ccclass('HeatDistortionManager')
export class HeatDistortionManager extends Component {
    @property(Material)
    heatMaterial: Material = null!;

    private heatData: Vec4 = undefined;
    private heatLife: number = 1.0 ;

    private time:number = 0;
    update(dt: number) {
        // const now = performance.now() / 1000;
        this.time += dt;
        if(!this.heatMaterial) return;
        let pass = this.heatMaterial.passes[0]
        if(!pass || !this.heatData) return;
        pass.setUniform(pass.getHandle('time'), this.time);
        pass.setUniform(pass.getHandle('heatData'), v4(this.heatData.x,this.heatData.y,0.15,1));
        pass.setUniform(pass.getHandle('heatLife'), this.heatLife);
    }

    public addHeatPoint(worldPos: Vec2) {
        // 转换为屏幕 UV（0~1）
        // const canvas = director.getScene().getComponentInChildren(UITransform)!;
        // let uitran = this.node.getComponent(UITransform)
        // if(!uitran) return;
        // const localPos = uitran.convertToNodeSpaceAR(worldPos);
        this.heatData =v4(worldPos.x,worldPos.y,0.15,1)
    }
}
